from CSVTools import CSVData

def csvStat_1( filename, data, names, acad_years ):

    #title = "Number of teachers in the higher education system per establishment per year"

    stats = []
    cdef int count = 0

    cdef int i = 0
    cdef int j = 0
  
    for i in xrange(0, len( names ) ):
        subdata = filter( lambda x: x[ 8 ] == names[ i ] , data )

        for j in xrange( 0, len( acad_years ) ):
            stats.append( [ names[ i ] ] )
            stats[ count ].append( acad_years[ j ] )
            stats[ count ].append( len( filter( lambda x: x[ 6 ] == acad_years[ j ], subdata ) ) )
            count += 1
            pass
        pass
    CSVData().writeCSV(filename + "_1", stats)
    pass
pass

def csvStat_2( filename, data, names, acad_years, categorys ):

    #title = "Number of teachers divided per categories per establisment and per year"
    
    stats = []
    cdef int count = 0

    cdef int i = 0
    cdef int j = 0
    cdef int k = 0
    
    for i in xrange( 0, len( names ) ):
        subdata = filter( lambda x: x[ 8 ] == names[ i ] , data )
        
        for j in xrange(0, len( acad_years )):
            aux = filter( lambda x: x[ 6 ] == acad_years[ j ], subdata )
            
            for k in xrange(0, len( categorys ) ):
                stats.append( [ names [ i ] ] )
                stats[ count ].append(acad_years[ j ] )
                stats[ count ].append(categorys[ k ] )
                stats[ count ].append( len( filter( lambda x: x[ 4 ] == categorys[ k ], aux ) ) )
                count += 1
            pass
        pass
    CSVData().writeCSV(filename + "_2", stats)
    pass
pass

def csvStat_3( filename, data, names, acad_years, grades ):

    #title = "Number of teachers divided per type of degree per establishment and per year)"

    stats = []
    cdef int count = 0

    cdef int i = 0
    cdef int j = 0
    cdef int k = 0
    
    for i in xrange( 0, len( names ) ):
        subdata = filter( lambda x:  x[ 8 ] == names[ i ] , data )
        
        for j in xrange(0, len( acad_years ) ):
            aux = filter( lambda x:  x[ 6 ] == acad_years[ j ] , subdata )
            
            for k in xrange( 0, len( grades ) ):
                stats.append( [ names [ i ] ] )
                stats[ count ].append( acad_years[ j ] )
                stats[ count ].append( grades[ k ] )
                stats[ count ].append( len( filter( lambda x: x[ 2 ] == grades[ k ] , aux ) ) )
                count += 1
            pass
        pass

    CSVData().writeCSV( filename + "_3", stats )
    pass
pass

def HTMLStatistics_1( self, data, stabNames, acad_years ):

    title = "Teachers in the higher education system per establishment per year"

    stats = []
    cdef int count = 0

    cdef int i = 0
    cdef int j = 0
    cdef int k = 0

    for i in xrange( 0, len( stabNames ) ):
        subdata = filter( lambda x: x[ 8 ] == stabNames[ i ] , data )
        
        for j in xrange( 0, len( acad_years) ):
            aux = filter( lambda x: x[ 6 ] == acad_years[ j ], subdata )
            
            for k in xrange( 0, len( aux ) ):
                stats.append( [ stabNames[ i ] ] )
                stats[ count ].append( acad_years[ j ] )
                stats[ count ].append( aux[ k ][ 1 ] )
                count += 1
                pass
            pass
        pass
    return self.tableHTML( title, stats )
pass


def HTMLStatistics_2( self, data, stabNames, acad_years, categorys ):

    title = "Teachers divided per categories per establisment and per year"

    stats = []
    cdef count = 0

    cdef int i = 0
    cdef int j = 0
    cdef int k = 0
    cdef int l = 0
    
    for i in xrange( 0, len( stabNames ) ):
        subdata = filter( lambda x: x[ 8 ] == stabNames[ i ] , data )
        
        for j in range( 0, len( acad_years ) ):
            aux = filter( lambda x: x[ 6 ] == acad_years[ j ] , subdata )
            
            for k in range( 0, len( categorys) ):
                aux2 = filter( lambda x: x[ 4 ] == categorys[ k ], aux )
                
                for l in range(0, len( aux2 ) ):
                    stats.append( [ stabNames[ i ] ] )
                    stats[ count ].append( acad_years[ j ] )
                    stats[ count ].append( categorys[ k ] )
                    stats[ count ].append( aux2[ l ][ 1 ] )
                    count += 1
                    pass
                pass
            pass
        pass
    return self.tableHTML( title, stats )
pass

def HTMLStatistics_3( self, data, stabNames, acad_years, grades ):

    title = "Teachers divided per type of degree per establishment and per year"

    stats = []
    cdef int count = 0

    cdef int i = 0
    cdef int j = 0
    cdef int k = 0
    cdef int l = 0
    
    for i in xrange( 0, len( stabNames ) ):
        subdata = filter( lambda x: x[ 8 ] == stabNames[ i ] , data )
        
        for j in xrange( 0, len( acad_years) ):
            aux = filter( lambda x: x[ 6 ] == acad_years[ j ] , subdata )
            
            for k in xrange( 0, len( grades ) ):
                aux2 = filter( lambda x: x[ 2 ] == grades[ k ] , aux)
                
                for l in range( 0, len( aux2 ) ):
                    stats.append( [ stabNames[ i ] ] )
                    stats[ count ].append( acad_years[ j ] )
                    stats[ count ].append( grades[ k ] )
                    stats[ count ].append( aux2[ l ][ 1 ] )
                    count += 1
                    pass
                pass
            pass
        pass
    return self.tableHTML( title, stats )
pass

def graphStats( index, data ):

    x = []
    y = []

    cdef int i = 0
    
    for i in xrange( 0, len( data ) ):
        x.append( int( data[ i ][ 1 ] ) )
        y.append( int( data[ i ][ index ] ) )
        pass
    return [ x, y ]
pass
